package com.laicunba.ext.activity.act20170703;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.laicunba.pojo.ActivityDO;
import com.laicunba.pojo.UserDO;
import com.laicunba.service.VcoinWalletService;
import com.laicunba.service.activity.ActivityChanceService;
import com.laicunba.util.CacheUtil;
import com.laicunba.util.DateUtil;
import com.laicunba.util.StringUtil;

//@Service
public class Act20170703ChanceService implements ActivityChanceService {

	@Autowired
	private VcoinWalletService vcoinWalletService;
	
	@Override
	public String getActivityId() {
		return "20170703";
	}

	@Override
	public boolean hasChance(UserDO userDO, ActivityDO activityDO, String subActId) {
		if ("011".equalsIgnoreCase(subActId)) {
			String regChanceKey = "act.20170703chance." + userDO.getId() + ".regist";
			String cjCountKey = "act.20170703CJ." + userDO.getId() + "." + DateUtil.formatDate(new Date());
			String chanceKey = "act.20170703chance." + userDO.getId()+ "." + DateUtil.formatDate(new Date());
			int regChance = StringUtil.isNotEmpty(CacheUtil.getAttribute(regChanceKey)) ? Integer.parseInt(CacheUtil.getAttribute(regChanceKey)) : 0 ;
			int todayTotalChance = StringUtil.isNotEmpty(CacheUtil.getAttribute(chanceKey)) ? Integer.parseInt(CacheUtil.getAttribute(chanceKey)) : 0 ;
			int todayCjCount = StringUtil.isNotEmpty(CacheUtil.getAttribute(cjCountKey)) ? Integer.parseInt(CacheUtil.getAttribute(cjCountKey)) : 0 ;
			//没有抽奖机会且积分<300，无法进行抽奖
			if (regChance <= 0 && todayTotalChance - todayCjCount <= 0 && vcoinWalletService.getBalance(userDO.getId()) < 300) {
				return false;
			}
		}
		return true;
	}

}
